#include "CEffectManager.h"

CEffectManager* CEffectManager::m_pEffect = NULL;


CEffectManager::CEffectManager()
{
	m_pEffecInfo		= NULL;
	m_nEffectInfoCount	= 0;
	m_nCount			= 0;
}

CEffectManager::~CEffectManager()
{
	if( m_pEffecInfo )
		delete [] m_pEffecInfo;

	m_pEffecInfo		= NULL;
	m_nEffectInfoCount	= 0;

	m_listEffect.clear();
	m_nCount = 0;
}



//--------------------------------------//
// Name : GetInstance					//
// Desc : Get instance of this class	//
//--------------------------------------//
CEffectManager* CEffectManager::GetInstance()
{
	if( m_pEffect == NULL )
	{
		m_pEffect = new CEffectManager();
	}

	return m_pEffect;
}




//--------------------------------------//
// Name : Init							//
// Desc : Init list effect information	//
//--------------------------------------//
void CEffectManager::Init( LPCTSTR DefineFile )
{
	char	Buffer[1025];
	char	Section[100];
	char	keyName[100];
	int		index , imageIndex , start , end , loop , numFrame , numFrameInRow , width , height , offsetWidth , offsetHeight; 
	float	speedTop , speedBot , offsetX , offsetY;

	strcpy( Section , "EffectInfo" );

	m_nEffectInfoCount = GetPrivateProfileInt( Section , "EffectCount" , 0 , DefineFile );
	m_pEffecInfo = new EFFECT_INFO[m_nEffectInfoCount];

	for( ULONG i = 0 ; i < m_nEffectInfoCount ; ++i)
	{
		_stprintf( keyName , _T("Effect_%i") , i );
		GetPrivateProfileString( Section , keyName , "0, 0, 0, 0, 0, 0/1, 0, 0, 0, 0, 0, 0, 0, 0" , Buffer , 1024 , DefineFile );
		sscanf( Buffer , "%i,%i,%i,%i,%i,%f/%f,%i,%i,%f,%f,%i,%i,%i,%i" , &index , &imageIndex , &start , &end , &loop , &speedTop , &speedBot , &numFrame , &numFrameInRow , &offsetX , &offsetY , &offsetWidth , &offsetHeight , &width , &height );
		m_pEffecInfo[i].index			= index;
		m_pEffecInfo[i].imageIndex		= imageIndex;
		m_pEffecInfo[i].numFrame		= numFrame;
		m_pEffecInfo[i].numFrameInRow	= numFrameInRow;
		m_pEffecInfo[i].width			= width;
		m_pEffecInfo[i].height			= height;
		m_pEffecInfo[i].offsetX			= offsetX;
		m_pEffecInfo[i].offsetY			= offsetY;
		m_pEffecInfo[i].offsetWidth		= offsetWidth;
		m_pEffecInfo[i].offsetHeight	= offsetHeight;
		m_pEffecInfo[i].startFrame		= start;
		m_pEffecInfo[i].endFrame		= end;
		m_pEffecInfo[i].loopFrame		= loop;
		m_pEffecInfo[i].actionSpeed		= speedTop/speedBot;
	}
}




void CEffectManager::StartEffect(EFFECT_INDEX index, float posX, float posY, float velX , float velY , float timeLife)
{
	EFFECT_INFO pEffectInfo = m_pEffecInfo[index];
	CEffect* pNewEffect;
	CAnimation* pAnim;

	pAnim = new CAnimation[1];
	pAnim[0].CreateAnimation( 0 , pEffectInfo.startFrame , pEffectInfo.endFrame , pEffectInfo.loopFrame , pEffectInfo.actionSpeed );
	pNewEffect = new CEffect();

	pNewEffect->SetPosition( posX , posY );
	pNewEffect->SetVelocity( velX , velY );
	pNewEffect->SetSize( pEffectInfo.width , pEffectInfo.height );
	pNewEffect->LoadSprite( &CResourceManager::GetInstance()->m_pListImage[pEffectInfo.imageIndex] , pEffectInfo.numFrame , pEffectInfo.numFrameInRow );
	pNewEffect->GetSprite()->SetAnimation( pAnim );
	pNewEffect->SetTimeLife( timeLife );
	pNewEffect->CreateBoundBox( pEffectInfo.offsetX , pEffectInfo.offsetY , pEffectInfo.offsetWidth , pEffectInfo.offsetHeight );

	m_listEffect.push_back( pNewEffect );
	m_nCount+=1;
}


void CEffectManager::UpdateEffect(float deltaTime)
{
	if( m_nCount <= 0 )
		return;

	for( ULONG i = 0 ; i < m_nCount ; ++i )
	{
		m_listEffect[i]->Update( deltaTime );
		if( !m_listEffect[i]->IsAlive( ) )
		{
			m_listEffect.erase( m_listEffect.begin() + i );
			m_nCount-=1;
		}
	}
}


void CEffectManager::DrawEffect(float camX /*= 0*/, float camY /*= 0*/)
{
	if( m_nCount <= 0 )
		return;

	for( ULONG i = 0 ; i < m_nCount ; ++i )
	{
		m_listEffect[i]->Render( camX , camY );
	}
}